package com.block.risk.service.type.item;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.block.risk.model.result.RiskJudgeResult;
import com.block.risk.service.IDefaultItemService;
import org.springframework.stereotype.Service;

@Service
public class XinNianItemService implements IDefaultItemService {
    @Override
    public RiskJudgeResult getControlResult(JSONObject resultDetial) {

        /**
         * 芝麻信用分600+ 近一个月了履约1笔以上，有借款，近六个月没有逾期
         */

        RiskJudgeResult riskJudgeResult = new RiskJudgeResult();
        riskJudgeResult.setPass(false);

        JSONObject currentReportDetail = resultDetial.getJSONObject("current_report_detail");
        JSONObject behaviorReportDetail = resultDetial.getJSONObject("behavior_report_detail");
        JSONObject applyReportDetail = resultDetial.getJSONObject("apply_report_detail");

        if (ObjectUtil.isEmpty(behaviorReportDetail)){
            riskJudgeResult.setResult("行为数据结果为空；【behavior_report_detail】");
            return riskJudgeResult;
        }

        // B22170045 近1 个月履约贷 款次数
        String repayNumber = behaviorReportDetail.getString("B22170045");
        if (Integer.valueOf(repayNumber) < 1){
            riskJudgeResult.setResult(StrUtil.format("近1 个月履约贷 款次数2笔以上不通过；【{}】", repayNumber));
            return riskJudgeResult;
        }

        String loanNumber = behaviorReportDetail.getString("B22170002");
        if (Integer.valueOf(loanNumber) < 1){
            riskJudgeResult.setResult(StrUtil.format("近 1个月贷款笔数1笔以上不通过；【{}】", loanNumber));
            return riskJudgeResult;
        }

        String numberRate = behaviorReportDetail.getString("B22170034");
        numberRate = numberRate.replace("%", "");
        if (Integer.valueOf(numberRate) < 95){
            riskJudgeResult.setResult(StrUtil.format("订单比例90以上不通过；【{}】", numberRate));
            return riskJudgeResult;
        }

        riskJudgeResult.setPass(true);
        riskJudgeResult.setResult("全景分控通过");
        return riskJudgeResult;
    }
}
